package club.hicode.daydayup.single;

/**
 * 单例的第三种写法
 *
 * @author Liu Chunfu
 * @date 2018-06-13 下午5:55
 **/
public class SinglePerson3 {

    //此处要防止指令重排序，导致获取的对象为 null
    private static volatile SinglePerson3 singlePerson3;

    public static SinglePerson3 single() {
        if (singlePerson3 == null) {
            synchronized (SinglePerson3.class) {
                if (singlePerson3 == null) {
                    singlePerson3 = new SinglePerson3();
                }
            }
        }
        return singlePerson3;
    }

    private SinglePerson3() {
    }
}
